<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Member\Controller;
use \Member\Controller\InitController;
class RecommendController extends InitController {
	
	public function _initialize() {
		parent::_initialize();
		$this->db = D('Member');
		$this->info_db = D('Member_detail');
		$this->score = D('Score');
		
	}

	public function index(){
		$this->re_link();
	}
	
	//推荐好友链接
	public function re_link() {
		$catid = 52;
		$SEO=seo(0,"推荐好友链接");
		$starttime = strtotime(date('Y-m-01',time()));
		$endtime = strtotime(date('Y-m-t',time()));
		$sql_map = "posttime > ".$starttime." AND posttime < ".$endtime." AND status = 1 AND u_type = 'rec' AND userid = ".$this->userid;
	 	$infos = $this->score->field("SUM(`change`)")->where($sql_map)->find();
        
		
	 	$changes = $infos['SUM(`change`)'];
		include template('list_re_link');
	}
	
	//推荐好友奖励
	public function re_reward() {
		$catid = 52;
		$SEO=seo(0,"推荐好友奖励");
		$config = getcache('setting', 'member');
		$counts = $this->db->where(array('agent_id'=>$this->userid))->count();
		$sql_map = array();
		$sql_map['u_type'] = 'rec';
		$sql_map['userid'] = $this->userid;
		$sql_map['status'] = '1';
		$total_coin = $this->score->where($sql_map)->sum('`change`');

		$arr = array();
		$arr['regdate'] = array(array('EGT',strtotime(date('Y-m-d 00:00:00',time()))),array('ELT',strtotime(date('Y-m-d 23:59:59',time()))));
		$arr['agent_id'] = $this->userid;
		$re_userid = $this->db->where($arr)->count();

		$res = array();
		$res['posttime'] = array(array('EGT',strtotime(date('Y-m-d 00:00:00',time()))),array('ELT',strtotime(date('Y-m-d 23:59:59',time()))));
		$res['userid'] = $this->userid;
		$res['u_type'] = 'rec';
		$res['status'] = '1';
		$today_coin = $this->score->where($res)->sum('`change`');

		include template('list_re_reward');
	}
	
	//推荐四级好友详解
	public function re_four() {
		$catid = 52;
		$SEO=seo(0,"推荐四级好友详解");
		include template('list_re_four');
	}
	
	//查看我的好友
	public function re_list(){
		$catid = 52;
		$SEO=seo(0,"我的推荐好友信息");
		$info=I('get.');
		$pagecurr = max(1,I('page',0,'intval'));
		$pagesize = 10;
		$sqlmap=array();
		$sqlmap['agent_id']=$this->userid;
		if ($info['method']== 1){
			$sqlmap['userid']=$info['value'];
		
		}elseif($info['method']== 3){
			$regtime=strtotime($info['value']);
			$sqlmap['posttime']=array(array('EQ',$regtime));
		}
		$count=$this->db->where($sqlmap)->count();
		$list=$this->db->where($sqlmap)->page($pagecurr,$pagesize)->select();
		foreach ($list as $v){
		  $qq=$this->info_db->where(array('userid'=>$v['userid']))->select();
		}
		$pages = page($count,$pagesize);
		$cash_reward=$this->score->where(array('u_type'=>'rec','userid'=>$this->userid))->sum('`change`');
		include template('list_re_list');
	}
	
	
	
	public function re_sucai(){
		$catid = 52;
		$SEO=seo(0,"推广素材");
		$filename = '{THEME_STYLE_PATH}style/images/tgsc/';
		include template('list_re_sucai');
	}
	
	public function upload(){
	 $name=I('get.');
	 $url=$name['picurl'];
	 $filename = '{THEME_STYLE_PATH}style/images/tgsc/';
	 //文件的类型
	 header('Content-type: application/pdf');
	//下载显示的名字
	 header("Content-Disposition: attachment; filename='$url'");
	 readfile("$filename");
	 exit(); 
	}

	
	public function match(){
	    $sql_map = "c_type =1  AND u_type = 'rec'";
        $infos = $this->score->where($sql_map)->limit(0,20)->order('`change` DESC')->select();
        $user = array();
        foreach ($infos as $key => $v) {
            $user[] = $v['userid'];
        }
        $user = array_unique($user);
        $tmp = array();
        $tmp['c_type'] = '1';
        $tmp['u_type'] = 'rec';
        $tmp['userid'] = array("IN",$user);
        $infos = $this->score->field("userid,SUM(`change`) as total")->where($tmp)->limit(0,20)->order('`change` DESC')->group('userid')->select();
        $users['userid'] = array("IN",$user);
        $user_info = $this->db->where($users)->getField('userid,nickname');
        for($i=0;$i<count($infos);$i++){
            foreach ($user_info as $k => $t) {
                if($k == $infos[$i]['userid'])
                    $infos[$i]['nickname'] = $t;
                    $infos[$i]['change'] = $infos[$i]['SUM(`change`)'];
            }
        }
        $condition=array();
        $condition['agent_id']=array('GT',0);
        $countlist=$this->db->field('agent_id,nickname,COUNT(agent_id) as count')->where($condition)->order('COUNT(agent_id) DESC')->group('agent_id')->limit(0,20)->select();
	    include template('list_re_match');
	}
	
	
}